<?php

class User extends CI_Model {

    function getPhotoAlbums($aid, $controller) {
        //get photo of all albums
        $query = "SELECT caption,link, src, pid, src_small , src_big, aid
                  FROM photo
                  WHERE aid = '$aid'";

        $param = array(
            'method' => 'fql.query',
            'query' => $query
        );
        $photo_albums = $controller->facebook->api($param);
        return $photo_albums;
    }

    function getFbAlbums($controller) {
        $result = array();
        $coverIds = "";
        $query = 'SELECT aid,owner,name,type,photo_count,cover_object_id, object_id
            FROM album
            WHERE owner=me()'
        ;
        $param = array(
            'method' => 'fql.query',
            'query' => $query
        );
        $albums = $controller->facebook->api($param);
        $albumIds = "";
        foreach ($albums as $album) {
            if ($album['cover_object_id']) {
                $coverIds .= $album['cover_object_id'];
                $coverIds .= ',';
            }
        }

        $coverIds = rtrim($coverIds, ',');
        $query = "SELECT link, src, pid, src_small , src_big, aid
                  FROM photo
                  WHERE object_id IN ($coverIds)";

        $param = array(
            'method' => 'fql.query',
            'query' => $query
        );
        $photos = $controller->facebook->api($param);
        $coverPhotos = array();
        if ($photos) {
            foreach ($photos as $photo) {
                foreach ($albums as $album) {

                    if ($photo['aid'] == $album['aid']) {
                        $photo['albumName'] = $album['name'];
                        $photo['count'] = $album['photo_count'];
                        $photo['albumObjectId'] = $album['object_id'];
                        $coverPhotos[] = $photo;
                    }
                }
            }
        }
        $result["coverPhotos"] = $coverPhotos;
        return $result;
    }

}